proProcess.deptIds1=[];

$(function(){

    $(document).delegate('.select2','click',function(){
        var $this = $(this);
        var $pro1 = $this.parents('.pro1');
        if( !$this.hasClass('select2-container--disabled') ){
            $pro1.addClass('show');
            proProcess.selectDept1();
        }
    });
    $(document).click(function(e){
        var target = e.target;
        if( $(target).parents('.por1').length ===0 ){
            $('.por1').removeClass('show');
        }
    });

});


proProcess.selectDept1=function(){
    var departments = $("#department_select1").select2("data");
    proProcess.getNewDate1(departments);
    $('.por1').addClass('show');
};

proProcess.getNewDate1=function(datas){
    var deptDatas = [];
    if(datas){
        for(var i = 0;i<datas.length;i++){
            deptDatas.push(datas[i].id);
        }
    }
    proProcess.deptIds1 = deptDatas;
};

// 确定受训角色
proProcess.finish1 = function(){
    var arr_ = [];
    var treeObj = $.fn.zTree.getZTreeObj("group_tree1");
    var checkedNodes = treeObj.getCheckedNodes(true);
    for(var i=0; i<checkedNodes.length; i++){
        if(checkedNodes[i].departmentId == undefined || '' == checkedNodes[i].departmentId){
            arr_.push(checkedNodes[i].companyId);
        }else{
            arr_.push(checkedNodes[i].departmentId);
        }
    }
    $("#department_select1").val(arr_).trigger("change");
    proProcess.close1();
    return false;
}

//隐藏树下拉框
proProcess.close1 = function(){
    $('.por1').removeClass('show');
    return false;
}

proProcess.groupTreeChecked1  = function(treeObj,treeNode){
    if(!$("#isChecked").is(":checked")){
        var nodes = treeNode.children;
        if(nodes){
            for(var i=0; i< nodes.length; i++) {
                if(nodes[i] && nodes[i].type == 2){
                    proProcess.dis_node1(treeObj,nodes[i]);
                }
            }
        }
    }
}

proProcess.dis_node1 = function (treeObj,node) {
    treeObj.checkNode(node, false, false, true);
    var children = node.children;
    if(children){
        for(var j=0; j< children.length; j++) {
            proProcess.dis_node1(treeObj,children[j]);
        }
    }
};

proProcess.initData1 = function (data) {
    var result = data.result;
    var treeObj = $.fn.zTree.getZTreeObj("group_tree1");
    //初始化默认所选单位选中状态
    for(var i = 0; i< result.length ;i++){
        var companyId = result[i].companyId
        var deptId = result[i].deptId;
        var node = treeObj.getNodeByParam("id", deptId, null);
        if(node!=null){
            node.checked = true ;
            treeObj.updateNode(node);
        }
    }
}

proProcess.check1 =function (responseData) {
    var result = proProcess.data1.result;
    for(var i =0; i < responseData.length; i++) {
        for(var j = 0;j<result.length;j++){
            if(responseData[i].departmentId == result[j].deptId){
                responseData[i].checked = true;
                //responseData[i].getParentNode.open =  true;
                break;
            }
        }
    }
    return responseData;
}

//初始化受训单位列表
proProcess.initDepartment1=function() {
    //console.log( $("#department_select2") );
    $("#department_select1").select2();
    $.ajax({
        url: proProcess.basePath + 'admin/project_create/private/trainCompany',
        dataType: 'json',
        type:'get',
        success:function (data) {
            var list=data.result;
            $("#department_select1").select2({
                tags:true,
                multiple:true,
                data:list,
                allowClear:true,
                placeholder:"请选择受训单位",
                language:"zh-CN"
            })
            //发送异步请求项目下已有的单位
            $.ajax({
                url: proProcess.basePath + 'admin/project_create/private/projectDepartment',
                dataType: 'json',
                type:'get',
                data:{"projectId":proProcess.projectId},
                success:function (data) {
                    proProcess.data1 = data;
                    var list=data.result;
                    var len=list.length;
                    var inner = "", item;
                    // 组装数据
                    for(var i=0; i< len; i++) {
                        item = list[i];
                        proProcess.deptIds1.push(item.deptId)
                    }
                    //$("#department_select2").val(proProcess.deptIds).trigger("change");
                    proProcess.initData1(data);
                    //点击小X触发，取消树中节点的勾选状态
                    proProcess.deleteDept1();

                }

            })

        }

    })

}

//点击小X触发，取消树中节点的勾选状态
proProcess.deleteDept1 = function () {
    $(document).delegate('.select2-selection__choice__remove','click',function(){
        var id = $(this).parent().attr('id');
        var treeObj = $.fn.zTree.getZTreeObj(tree_options.id);
        var node = treeObj.getNodeByParam("id", id, null);
        node.checked = false ;
        treeObj.updateNode(node);
    })
}
